<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>课程管理系统 - 控制端</title>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
    <style>
        * {
            box-sizing: border-box;
            margin: 0;
            padding: 0;
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
        }
        
        body {
            background: linear-gradient(135deg, #6a11cb 0%, #2575fc 100%);
            color: #333;
            line-height: 1.6;
            padding: 20px;
            min-height: 100vh;
        }
        
        .container {
            max-width: 1200px;
            margin: 0 auto;
            background: white;
            border-radius: 15px;
            box-shadow: 0 10px 30px rgba(0, 0, 0, 0.15);
            overflow: hidden;
        }
        
        header {
            background: #4a6fc3;
            color: white;
            padding: 25px;
            text-align: center;
        }
        
        h1 {
            font-size: 2.2rem;
            margin-bottom: 10px;
        }
        
        .subtitle {
            font-size: 1.1rem;
            opacity: 0.9;
        }
        
        .content {
            padding: 30px;
        }
        
        .form-group {
            margin-bottom: 20px;
        }
        
        label {
            display: block;
            margin-bottom: 8px;
            font-weight: 600;
            color: #2c3e50;
        }
        
        input, select {
            width: 100%;
            padding: 12px 15px;
            border: 2px solid #ddd;
            border-radius: 6px;
            font-size: 1rem;
            transition: border-color 0.3s;
        }
        
        input:focus, select:focus {
            border-color: #4a6fc3;
            outline: none;
        }
        
        .btn {
            padding: 12px 20px;
            border: none;
            border-radius: 6px;
            font-size: 1rem;
            font-weight: 600;
            cursor: pointer;
            transition: all 0.3s;
        }
        
        .btn-primary {
            background: #4a6fc3;
            color: white;
        }
        
        .btn-primary:hover {
            background: #3a5ba0;
        }
        
        .btn-danger {
            background: #ff4757;
            color: white;
        }
        
        .btn-danger:hover {
            background: #e8414d;
        }
        
        .btn-success {
            background: #2ed573;
            color: white;
        }
        
        .btn-success:hover {
            background: #27ae60;
        }
        
        .course-list {
            margin-top: 30px;
        }
        
        .course-item {
            display: flex;
            justify-content: space-between;
            align-items: center;
            padding: 15px;
            background: #f9f9f9;
            border-radius: 8px;
            margin-bottom: 15px;
            box-shadow: 0 2px 5px rgba(0,0,0,0.05);
        }
        
        .course-info {
            flex: 1;
        }
        
        .course-name {
            font-weight: 600;
            font-size: 1.1rem;
            color: #2c3e50;
        }
        
        .course-capacity {
            color: #7f8c8d;
            margin-top: 5px;
        }
        
        .course-actions {
            display: flex;
            gap: 10px;
        }
        
        .share-section {
            margin-top: 40px;
            padding: 20px;
            background: #f0f5ff;
            border-radius: 10px;
        }
        
        .share-url {
            display: flex;
            margin-top: 15px;
        }
        
        .url-input {
            flex: 1;
            padding: 10px;
            border: 1px solid #ddd;
            border-radius: 4px 0 0 4px;
            font-family: monospace;
        }
        
        .copy-btn {
            background: #4a6fc3;
            color: white;
            border: none;
            padding: 10px 15px;
            border-radius: 0 4px 4px 0;
            cursor: pointer;
        }
        
        .message {
            padding: 15px;
            margin-top: 20px;
            border-radius: 8px;
            font-weight: 500;
            text-align: center;
        }
        
        .error {
            background-color: #ffebee;
            color: #ff4757;
        }
        
        .success {
            background-color: #e3f2fd;
            color: #3498db;
        }
        
        .stats {
            display: flex;
            justify-content: space-around;
            margin-top: 30px;
            padding: 20px;
            background-color: #e3f2fd;
            border-radius: 8px;
        }
        
        .stat-item {
            text-align: center;
        }
        
        .stat-value {
            font-size: 2rem;
            font-weight: 700;
            color: #3498db;
        }
        
        .stat-label {
            color: #57606f;
        }
        
        @media (max-width: 768px) {
            .course-item {
                flex-direction: column;
                align-items: flex-start;
                gap: 15px;
            }
            
            .course-actions {
                align-self: flex-end;
            }
            
            .stats {
                flex-direction: column;
                gap: 15px;
            }
            
            .share-url {
                flex-direction: column;
            }
            
            .url-input {
                border-radius: 4px 4px 0 0;
            }
            
            .copy-btn {
                border-radius: 0 0 4px 4px;
            }
        }
    </style>
</head>
<body>
    <div class="container">
        <header>
            <h1>课程管理系统 - 控制端</h1>
            <p class="subtitle">管理课程并生成家长报名链接</p>
        </header>
        
        <div class="content">
            <h2>课程管理</h2>
            <p>在此添加、编辑或删除课程，设置每门课程的最大报名人数。</p>
            
            <div class="form-group">
                <label for="courseName">课程名称</label>
                <input type="text" id="courseName" placeholder="输入课程名称">
            </div>
            
            <div class="form-group">
                <label for="courseCapacity">最大人数限制</label>
                <input type="number" id="courseCapacity" placeholder="输入最大人数" value="30" min="1">
            </div>
            
            <button class="btn btn-primary" id="addCourseBtn">添加课程</button>
            
            <div class="course-list" id="courseList">
                <!-- 课程列表将由JavaScript动态生成 -->
            </div>
            
            <div class="message" id="adminMessage" style="display: none;"></div>
            
            <div class="stats">
                <div class="stat-item">
                    <div class="stat-value" id="totalCourses">0</div>
                    <div class="stat-label">总课程数</div>
                </div>
                <div class="stat-item">
                    <div class="stat-value" id="totalCapacity">0</div>
                    <div class="stat-label">总容量</div>
                </div>
                <div class="stat-item">
                    <div class="stat-value" id="totalEnrollments">0</div>
                    <div class="stat-label">总报名人数</div>
                </div>
            </div>
            
            <div class="share-section">
                <h2>家长报名链接</h2>
                <p>复制以下链接分享给家长进行课程报名：</p>
                
                <div class="share-url">
                    <input type="text" class="url-input" id="parentUrl" value="加载中..." readonly>
                    <button class="copy-btn" onclick="copyUrl()">复制链接</button>
                </div>
                
                <p style="margin-top: 15px; font-size: 0.9rem; color: #7f8c8d;">
                    <i class="fas fa-info-circle"></i> 将此链接发送给家长，家长可通过该链接访问报名页面。
                </p>
            </div>
        </div>
    </div>

    <script>
        // 初始化数据
        let courses = JSON.parse(localStorage.getItem('courses')) || [
            { id: 1, name: "高等数学", capacity: 30, enrolled: 0 },
            { id: 2, name: "大学英语", capacity: 30, enrolled: 0 },
            { id: 3, name: "计算机科学", capacity: 30, enrolled: 0 }
        ];
        
        let students = JSON.parse(localStorage.getItem('students')) || [];

        // 页面加载时初始化
        document.addEventListener('DOMContentLoaded', function() {
            renderCourses();
            updateStats();
            updateParentUrl();
        });

        // 渲染课程列表
        function renderCourses() {
            const courseList = document.getElementById('courseList');
            courseList.innerHTML = '';
            
            if (courses.length === 0) {
                courseList.innerHTML = '<p>暂无课程，请添加课程。</p>';
                return;
            }
            
            courses.forEach(course => {
                const courseItem = document.createElement('div');
                courseItem.className = 'course-item';
                courseItem.innerHTML = `
                    <div class="course-info">
                        <div class="course-name">${course.name}</div>
                        <div class="course-capacity">已报名: ${course.enrolled}/${course.capacity}</div>
                    </div>
                    <div class="course-actions">
                        <button class="btn btn-danger" onclick="deleteCourse(${course.id})">删除</button>
                    </div>
                `;
                courseList.appendChild(courseItem);
            });
            
            updateStats();
        }

        // 更新统计信息
        function updateStats() {
            const totalCourses = courses.length;
            const totalCapacity = courses.reduce((sum, course) => sum + course.capacity, 0);
            const totalEnrollments = courses.reduce((sum, course) => sum + course.enrolled, 0);
            
            document.getElementById('totalCourses').textContent = totalCourses;
            document.getElementById('totalCapacity').textContent = totalCapacity;
            document.getElementById('totalEnrollments').textContent = totalEnrollments;
        }

        // 更新家长端URL
        function updateParentUrl() {
            // 获取当前页面的URL并替换为家长端页面
            const currentUrl = window.location.href;
            const parentUrl = currentUrl.replace('admin.html', 'parent.html');
            document.getElementById('parentUrl').value = parentUrl;
        }

        // 添加课程
        document.getElementById('addCourseBtn').addEventListener('click', function() {
            const courseName = document.getElementById('courseName').value.trim();
            const courseCapacity = parseInt(document.getElementById('courseCapacity').value);
            
            if (!courseName) {
                showMessage('adminMessage', '请输入课程名称', 'error');
                return;
            }
            
            if (isNaN(courseCapacity) || courseCapacity < 1) {
                showMessage('adminMessage', '请输入有效的人数限制', 'error');
                return;
            }
            
            const newCourse = {
                id: Date.now(), // 使用时间戳作为唯一ID
                name: courseName,
                capacity: courseCapacity,
                enrolled: 0
            };
            
            courses.push(newCourse);
            saveData();
            renderCourses();
            document.getElementById('courseName').value = '';
            showMessage('adminMessage', '课程添加成功', 'success');
        });

        // 删除课程
        function deleteCourse(courseId) {
            // 检查是否有学生报名了该课程
            const course = courses.find(c => c.id === courseId);
            if (course.enrolled > 0) {
                showMessage('adminMessage', '无法删除已有学生报名的课程', 'error');
                return;
            }
            
            courses = courses.filter(c => c.id !== courseId);
            saveData();
            renderCourses();
            showMessage('adminMessage', '课程删除成功', 'success');
        }

        // 复制URL
        function copyUrl() {
            const urlInput = document.getElementById('parentUrl');
            urlInput.select();
            document.execCommand('copy');
            showMessage('adminMessage', '链接已复制到剪贴板', 'success');
        }

        // 显示消息
        function showMessage(elementId, message, type) {
            const messageEl = document.getElementById(elementId);
            messageEl.textContent = message;
            messageEl.className = `message ${type}`;
            messageEl.style.display = 'block';
            
            // 3秒后隐藏消息
            setTimeout(() => {
                messageEl.style.display = 'none';
            }, 3000);
        }

        // 保存数据到本地存储
        function saveData() {
            localStorage.setItem('courses', JSON.stringify(courses));
            localStorage.setItem('students', JSON.stringify(students));
        }
    </script>
</body>
</html>